import Vuex from 'vuex'
import Vue from 'vue'

Vue.use(Vuex)
const store = new Vuex.Store({
  state: {
    token: '',
    userInfo: {},
    referId: 0
  },
  mutations: {
    setToken (state, token) {
      localStorage.setItem('token', token)
      this.state.token = token
    },
    setUserInfo (state, data) {
      data = JSON.stringify(data)
      localStorage.setItem('userInfo', data)
      this.state.userInfo = data
    },
    setReferId (state, data) {
      localStorage.setItem('referId', data)
      this.state.referId = data
    }
  },
  getters: {
    getUserInfo: state => {
      state.userInfo = localStorage.getItem('userInfo')
      if (state.userInfo) {
        return JSON.parse(state.userInfo)
      } else {
        return state.userInfo
      }
    },
    token: state => {
      state.token = localStorage.getItem('token')
      return state.token
    },
    referId: state => {
      state.referId = localStorage.getItem('referId')
      return state.referId
    }
  }
});
export default store
